// components/preview-file/index.js
import {HTTP} from "../../utils/http"

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        fileInfo: Object
    },

    /**
     * 组件的初始数据
     */
    data: {
        ShowDownloadDialog:false
    },
    /**
     * 组件的方法列表
     */
    methods: {
        openFile() {
            let that=this
            wx.showLoading({
                title: '文件转码中...',
            })
            let allow_filetype = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf']
            let allow_imagetype = ['jpg','png','gif']
            let filetype = this.getFileType(this.properties.fileInfo.filename)
            if (allow_filetype.indexOf(filetype) !== -1) {
                wx.downloadFile({
                    url: this.properties.fileInfo.ossurl,
                    success: function (res) {
                        var filePath = res.tempFilePath
                        wx.openDocument({
                            filePath: filePath,
                            fileType: filetype,
                            success: function (res) {
                                wx.hideLoading()
                            }
                        })
                    }
                })
            } else if(allow_imagetype.indexOf(filetype) !== -1){
                wx.previewImage({
                    urls: [that.properties.fileInfo.ossurl],
                    success: function (res) {
                        wx.hideLoading()
                    }
                })
            }else{
                wx.hideLoading()
                wx.navigateTo({url: '/pages/web-view/web-view?furl=' + this.properties.fileInfo.ossurl})
            }
        },

        async deleteFile() {
            wx.showLoading({
                title: '文件删除...',
            })
            const res = await HTTP.request({
                url: 'wx/tasks/deletefiles',
                method: 'POST',
                data: {
                    id: this.properties.fileInfo.id,
                }
            })
            if (res.error_code === 0) {
                wx.hideLoading()
            }
        },

        downloadFile(){
            let that=this
            wx.setClipboardData({
                data: that.properties.fileInfo.ossurl,
            })
        },


        showActionSheet() {
            let that = this
            wx.lin.showActionSheet({
                itemList: [
                    {
                        name: '预览文件',
                        color: '#3683D6'
                    },
                    {
                        name:'下载文件到本地',
                        color: '#3683D6'
                    },
                    {
                        name: '删除文件',
                        color: '#F4516C',
                    }
                ],
                success(res) {

                    switch (res.index) {
                        case 0:
                            that.openFile();
                            break
                        case 1:
                            that.setData({
                                ShowDownloadDialog:true
                            })
                            break
                        case 2:
                            that.deleteFile()
                            break
                        default:
                            break
                    }
                }
            })
        },


        getFileType: function (filePath) {
            var startIndex = filePath.lastIndexOf(".");
            if (startIndex != -1)
                return filePath.substring(startIndex + 1, filePath.length).toLowerCase();
            else return "";
        }
    }
})
